package com.wanlian.context.Notice;

import com.wanlian.vo.NoticeMsgResultVo;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class NoticeMsgContext {

    private static Queue<NoticeMsgResultVo> noticesQueue = new LinkedList<NoticeMsgResultVo>();

    public static synchronized void add(NoticeMsgResultVo noticeMsg) {
        if (noticesQueue != null) {
            noticesQueue.offer(noticeMsg);
        }
    }

    public static synchronized NoticeMsgResultVo get() {
        if (noticesQueue != null) {
            if (noticesQueue.size() > 0) {
                NoticeMsgResultVo noticeMsg = noticesQueue.poll();
                if (noticeMsg.isAll()) {
                    return noticeMsg;
                }
                if (NoticeManager.canSend(noticeMsg.getUser().getId())) {
                    return noticeMsg;
                } else {
                    NoticeManager.add(noticeMsg.getUser().getId(), noticeMsg);
                    return null;
                }
            }
        }
        return null;
    }

    public static synchronized List<NoticeMsgResultVo> get(int count) {
        if (noticesQueue != null) {
            List<NoticeMsgResultVo> noticeMsgs = new ArrayList<>();
            int size = noticesQueue.size();
            count = count > size ? size : count;
            for (int i = 0; i < size; i++) {
                NoticeMsgResultVo noticeMsg = noticesQueue.poll();
                noticeMsgs.add(noticeMsg);
            }
            return noticeMsgs;
        }
        return null;
    }
}
